module dgl.graphics.LoadHelper;

import tango.text.convert.Float;
import tango.io.File;
import tango.text.Util;

import dgl.Vector2;
import dgl.Vector3;
import dgl.Vector4;
import dgl.Quaternion;

class LoadHelper
{
	private char[][] m_lines;

	private uint m_curLine;

	public this( char[] filename )
	{
		File file = new File( filename );
		m_lines = splitLines( cast(char[])file.read() );
		m_curLine = 0;
	}

	public char[] readLine( out char[] data )
	{
		char[] line;
		line = m_lines[m_curLine];
		int index = 0;
		do
		{
			index = indexOf!(char)( line.ptr, ' ', line.length );
		} while( index == line.length )
			
		data = line[index+1 .. line.length];
		m_curLine++;
		return line[0 .. index];
	}
	
	public static Vector2 stringsToVector2( char[] data )
	{
		char[][] elements = split( data, " " );
		if( elements.length == 2 )
		{
			Vector2 ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			return ret;
		}
		return Vector2.zero;
	}
	
	public static Vector3 stringsToVector3( char[] data )
	{
		char[][] elements = split( data, " " );
		if( elements.length == 3 )
		{
			Vector3 ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			ret.z = toFloat( elements[2] );
			return ret;
		}
		return Vector3.zero;
	}
	
	public static Vector4 stringsToVector4( char[] data )
	{
		char[][] elements = split( data, " " );
		if( elements.length == 4 )
		{
			Vector4 ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			ret.z = toFloat( elements[2] );
			ret.w = toFloat( elements[3] );
			return ret;
		}
		return Vector4.zero;
	}
	
	public static Quaternion stringsToQuaternion( char[] data )
	{
		char[][] elements = split( data, " " );
		if( elements.length == 4 )
		{
			Quaternion ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			ret.z = toFloat( elements[2] );
			ret.w = toFloat( elements[3] );
			return ret;
		}
		return Quaternion.identity;
	}
}